/**
 * @file channel.h
 * @author 王烨
 * @date 2011-01-05
**/

#ifndef CHANNEL_H_INCLUDED
#define CHANNEL_H_INCLUDED

#include <string>

#include <uuid/uuid.h>

#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <google/protobuf/service.h>

#include "message.pb.h"

namespace skeleton
{
  /*发送信道的基类方法*/
  class Channel : public google::protobuf::RpcChannel
  {
  public:
    /*调用RPC方法*/
    virtual void CallMethod(const google::protobuf::MethodDescriptor *method,
			    google::protobuf::RpcController *controller,
			    const google::protobuf::Message *parameters,
			    google::protobuf::Message *result, google::protobuf::Closure *done);
  protected:
    /*根据请求的方法描述和参数生成要发送的数据并返回*/
    std::string serializeRequest(const google::protobuf::MethodDescriptor &descriptor,
				 const google::protobuf::Message &parameters) const;
    /*根据请求的方法描述和参数生成要发送的数据并放进传进来的requestData字符串对象中*/
    void serializeRequest(const google::protobuf::MethodDescriptor &descriptor,
			  const google::protobuf::Message &parameters,
			  std::string &requestData) const;
    /*根据请求的方法描述和参数生成要发送的数据并放进传进来的requestData数组中*/
    void serializeRequest(const google::protobuf::MethodDescriptor &descriptor,
			  const google::protobuf::Message &parameters,
			  void *requestData,
			  int length) const;
    /*解析收到的响应到消息对象*/
    void deserializeResponse(const std::string &responseData,
			     uuid_t &uuid,
			     google::protobuf::Message &result) const;
    /*解析收到的响应到消息对象*/
    void deserializeResponse(const void *array,
			     int length,
			     uuid_t &uuid,
			     google::protobuf::Message &result) const;
  private:
    /*生成request对象*/
    void serializeRequest(const google::protobuf::MethodDescriptor &descriptor,
			  const google::protobuf::Message &parameters,
			  Request &request) const;
    /*解析收到的响应*/
    void deserializeResponse(const Response &response,
			     uuid_t &uuid,
			     google::protobuf::Message &result) const;
  };
}
#endif
